package com.example.index.service.impl;

import com.example.index.entity.HomeWork;
import com.example.index.dao.HomeWorkDao;
import com.example.index.service.HomeWorkService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 学生作品(HomeWork)表服务实现类
 *
 * @author makejava
 * @since 2023-08-29 17:13:56
 */
@Service("homeWorkService")
public class HomeWorkServiceImpl implements HomeWorkService {
    @Resource
    private HomeWorkDao homeWorkDao;

    /**
     * 通过ID查询单条数据
     *
     * @param imageId 主键
     * @return 实例对象
     */
    @Override
    public HomeWork queryById(Integer imageId) {
        return this.homeWorkDao.queryById(imageId);
    }

    /**
     * 分页查询
     *
     * @param homeWork 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<HomeWork> queryByPage(HomeWork homeWork, PageRequest pageRequest) {
        long total = this.homeWorkDao.count(homeWork);
        return new PageImpl<>(this.homeWorkDao.queryAllByLimit(homeWork, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param homeWork 实例对象
     * @return 实例对象
     */
    @Override
    public HomeWork insert(HomeWork homeWork) {
        this.homeWorkDao.insert(homeWork);
        return homeWork;
    }

    /**
     * 修改数据
     *
     * @param homeWork 实例对象
     * @return 实例对象
     */
    @Override
    public HomeWork update(HomeWork homeWork) {
        this.homeWorkDao.update(homeWork);
        return this.queryById(homeWork.getImageId());
    }

    /**
     * 通过主键删除数据
     *
     * @param imageId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer imageId) {
        return this.homeWorkDao.deleteById(imageId) > 0;
    }
}
